package along.com.myboxugu.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import along.com.myboxugu.R;

public class NewsActivity extends AppCompatActivity {
    WebView Textview;
    String contentid;
    TextView newsTv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news);
        Textview = (WebView) findViewById(R.id.web_view);
        newsTv= (TextView) findViewById(R.id.new_tv);
        contentid=getIntent().getStringExtra("contentid");
        nes();
        loading();
    }
    public  void  nes() {
        RequestParams params = new RequestParams("http://web.csmzxy.com/netCourse/readData");
        params.addQueryStringParameter("cmd", "41");
        params.addQueryStringParameter("v1", contentid);
        x.http().get(params, new Callback.CommonCallback<String>() {

            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                newsTitleModel newsModel =gson.fromJson(convertString(result),newsTitleModel.class);
                newsTv.setText(newsModel.contentTitle);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
        public String convertString(String result) {
            String result1 = result
                    .replace(", responseSign : 00", "")
                    .replace("\'", "\"")
                    .replace("contentID", "\"contentID\"")
                    .replace("contentAuthor", "\"contentAuthor\"")
                    .replace("contentTitle", "\"contentTitle\"")
                    .replace("submitTime", "\"submitTime\"");
            return result1;

        }
        public  void  loading(){
            RequestParams params = new RequestParams("http://web.csmzxy.com/netCourse/readData");
            params.addQueryStringParameter("cmd", "9");
            params.addQueryStringParameter("v1", contentid);
            x.http().get(params, new Callback.CommonCallback<String>() {

                @Override
                public void onSuccess(String result) {
                    String encoding="UTF-8";
                    String mimeType="text/html";

                    Textview.loadDataWithBaseURL("file://",result,mimeType,encoding,"about:blank");

                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                    Toast.makeText(x.app(),ex.getMessage(),Toast.LENGTH_LONG).show();
                }

                @Override
                public void onCancelled(CancelledException cex) {
                   Toast.makeText(x.app(),"cancelled",Toast.LENGTH_LONG).show();
                }

                @Override
                public void onFinished() {

                }
            });
           }

    }

